﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chessLogic
{
    public class Chess
    {
        public Board board;
        public FigureMove move;
        public Game currentGame;

        public Chess()
        {
            board = new Board();
            currentGame = new Game();
            move = new FigureMove();
        }

        public Position getOldPosition(FigureMove move)
        {
            List<Position> figurePositions = board.getFigurePositions(move.MovingFigure);
            if (move.MovingFigure.Type == FigureType.King)
                return board.getFigurePositions(move.MovingFigure)[0];

            foreach (Position pos1 in figurePositions)
            {
                if (move.MovingAdditChar != ' ' && !pos1.ContainChar(move.MovingAdditChar))
                    continue;

                List<Position> canPosition = new List<Position>();
                if (move.MovingType == MoveType.Normal)
                    canPosition = board.getFigure(pos1).GetPossiblyPositionNormalMove(pos1,  board);
                else if (move.MovingType == MoveType.Taking)
                    canPosition = board.getFigure(pos1).GetPossiblyPositionTakingMove(pos1,  board);
                else if (move.MovingType == MoveType.Passant)
                    canPosition = Pawn.GetPossiblyPositionPassantMove(pos1,  board, move.MovingFigure.Color);

                foreach (Position pos2 in canPosition)
                    if (pos2.Equals(move.MovingWhere))
                        return pos1;
            }
            return null;
        }
        public void MakeMove(FigureMove move)
        {
            if (move.MovingType == MoveType.KingCastling)
            {
                if (move.MovingFigure.Color == FigureColor.White)
                {
                    board.MoveFigure(new Position("e1"), new Position("g1"));
                    board.MoveFigure(new Position("h1"), new Position("f1"));
                }
                else
                {
                    board.MoveFigure(new Position("e8"), new Position("g8"));
                    board.MoveFigure(new Position("h8"), new Position("f8"));
                }
            }
            else if (move.MovingType == MoveType.QueenCastling)
            {
                if (move.MovingFigure.Color == FigureColor.White)
                {
                    board.MoveFigure(new Position("e1"), new Position("c1"));
                    board.MoveFigure(new Position("h1"), new Position("d1"));
                }
                else
                {
                    board.MoveFigure(new Position("e8"), new Position("c8"));
                    board.MoveFigure(new Position("h8"), new Position("d8"));
                }
            }
            else if (move.MovingType == MoveType.Passant)
            {
                Position oldPosition = getOldPosition(move);
                board.MoveFigure(oldPosition, move.MovingWhere);
                int increment = (move.MovingFigure.Color == FigureColor.White) ? -1 : 1;
                Position positionTake = new Position(move.MovingWhere.Letter, move.MovingWhere.Number + increment);
                board.setFigure(positionTake, new None());
            }
            else if (move.MovingFigurePromotion != null)
            {
                Position oldPosition = getOldPosition(move);
                board.MoveFigure(oldPosition, move.MovingWhere);
                board.setFigure(move.MovingWhere, move.MovingFigurePromotion);
            }
            else
            {
                Position oldPosition = getOldPosition(move);
                board.MoveFigure(oldPosition, move.MovingWhere);
            }
        }
        public void InitialChess()
        {
            board.InitialBoard();
            currentGame.MoveNumber = 0;
        }
        public void MakeNextMove()
        {
            if (!currentGame.IsFinished())
            { 
                FigureColor player = currentGame.getWhoMove();
                move.ConvertNotation(currentGame.NextMove(),  board, player);
                MakeMove(move);
            }
        }
    }
}
